<?php
/**
 * Description of ModuleController
 *
 * @author Pawy
 */

abstract class ModuleController extends MainController
{

    private $actionSet = array();

    public function SetActionSet($set) {
        $this->actionSet = $set;
    }

    public function LoadAdditionalModule($module, $controller) {
        if(is_file('controller/'.$module.'/'.str_replace($module.'_', '', ucfirst($controller)).'.php')) {
            Core::RequireOnce('controller/'.$module.'/'.str_replace($module.'_', '', ucfirst($controller)).'.php');
            MFLog::Debug('Loading module: '.$module.': '.$controller);
        } else {
            
            MFLog::Debug('Unable to load module, Loading controller: '.$controller);
            throw new CoreException('Unable to load module, Loading controller: '.$controller);
        }
    }
    
    public function RunAdditionalModule($controller, $method) {

        $controllerObj = null;

        if(class_exists($controller) && class_parents($controller) == Array('ModuleController'=>'ModuleController', 'MainController'=>'MainController', 'Controller'=>'Controller') && class_implements($controller) == Array('ControllerInterface'=>'ControllerInterface')) {
            $controllerObj = new $controller();

            if(!method_exists($controllerObj, $method) && !method_exists($controllerObj, $method.'Action')) {
                MFLog::Debug('Method thoes not exists: '.$method);
                throw CoreException('Method thoes not exists: '.$method);
            }
        } else {
            throw new CoreException('Brak klasy lub brak dziedziczenia!');
        }
        
        return $controllerObj;
    }

    public function DispatchAdditionalModule($controllerObj, $controller, $method, $module, $param) {
        $controllerObj->SetSmarty($this->smarty);
        $controllerObj->partialTemplate = $method.'.tpl';

        $controllerObj->templatePath = 'partial/'.str_replace('_', '/', str_replace('Controller', '', $controller)).'/';


        if((!$controllerObj->Init($method, md5(implode($param, '-'))) || $controllerObj->CheckLiveMethod($method)) && $controllerObj->GetActionRedirect()!=true) {

            $methodAction = $method;
            $controllerObj->$methodAction($param);

        }


        if(!isset($controllerObj->content)){
            $this->smarty->cache_lifetime = $controllerObj->cacheTime;
            $controllerObj->content
            = $this->smarty->fetch($controllerObj->templatePath.$controllerObj->partialTemplate, $method.substr(md5(implode($param, '-')),0,100));
            //echo $this->controllerObj->templatePath.$this->controllerObj->partialTemplate;

        }


        $this->smarty->append('zoneContent', $controllerObj->content);
    }

    public function GetActionSet() {
        return $this->actionSet;
    }

    public function ExecuteAdditionalActions($param) {
        $actionSetArray = $this->GetActionSet();
		$this->smarty->assign('zoneContent', '');

        foreach($actionSetArray as $actionSet) {
            $this->LoadAdditionalModule($actionSet['module'], $actionSet['controller']);
            $controllerObj = $this->RunAdditionalModule($actionSet['controller'], $actionSet['method']);
            $this->DispatchAdditionalModule($controllerObj, $actionSet['controller'], $actionSet['method'], $actionSet['module'], $param);
        }

    }
	


}
